<?php defined('BASEPATH') or exit('No direct script access allowed');

class Tournament_m extends MY_Model {	
	function __construct(){
		parent::__construct();
		$this->load->library('module_helper');
	}
	
	function setbetting(){
		$event_id = intval($_POST['event_id']);
		$stake = ($_POST['stake']);
		$points = ($_POST['points']);
		
		$action = array();
		$statusReturn = statusOfActionDataConfig();
	/*	if(!isUserLogin()){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['You_must_login_first']; 
			echo json_encode($action);
			exit();
		}
	*/	
		$EVENT = $this->event_entity->init('id', $event_id);
		$MATCH = $this->match_entity->init('id', $EVENT->getMatchId());
	/*	$USER = $this->user_entity->init("id", getUserId());
		if($USER->getCoin() == 0){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['You_have_no_coin_to_bet']; 
			echo json_encode($action);
			exit();
		}
	*/	
		if($EVENT->getBetEnable() != 'yes' OR $MATCH->getBetEnable() != 'yes'){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['Bet_of_this_event_is_disable']; 
			echo json_encode($action);
			exit();
		}
		
	/*	if( $EVENT->getEndTime() < time() ){	//$EVENT->getStartTime() > time() OR
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['Bet_of_this_event_is_outtime']; 
			echo json_encode($action);
			exit();
		}
	*/	
		//can bet here
		$uniqueid = $this->module_helper->getTokenId();
		$action['status'] = 'ok';
		$string = cutString($EVENT->getName(),6);
		$odds = bet_format($EVENT->getOdds()). ' P';
		$action['dom'] = "<div class=\"clrboth\"></div><div class=\"itembet\" id=\"event_{$uniqueid}\" rel=\"{$EVENT->getOdds()}\">";
		$action['dom'] .= "<div class=\"item_bet_grid_delete\" rel=\"{$uniqueid}\" title=\"Delete?\">&nbsp;</div>";
		$action['dom'] .= "<div class=\"item_bet_grid_info\" title=\"{$EVENT->getName()}\">{$string}</div>";
		$action['dom'] .= "<div class=\"item_bet_grid_info2 event_id_{$EVENT->getId()}\" rel=\"{$uniqueid}\">{$odds}</div>";
		$action['dom'] .= "<div class=\"clrboth\"></div>";
		$action['dom'] .= "<div class=\"item_bet_grid_odds\">
							<div style=\"float:left;\">	
								Stake <input type=\"text\" name=\"setodds[]\" value=\"{$stake}\" class=\"setoddsinput\" id=\"stake_{$uniqueid}\" rel=\"{$uniqueid}\" />
							</div>
							<div class=\"set-bet-sep-ip\" style=\"float:left;margin-left:40px;\">
								To Win <input type=\"text\" name=\"win[]\" value=\"{$points}\" class=\"setoddsinput2\" disabled=\"disabled\" id=\"win_{$uniqueid}\" rel=\"{$uniqueid}\"/>
							</div>
							<input type=\"hidden\" name=\"eventid[]\" value=\"{$EVENT->getId()}\" />
						</div>";
		$action['dom'] .= "</div><div class=\"clrboth\"></div>";		
		echo json_encode($action);
		exit();	
	}
	
	function placebet(){
		$action = array();
		$statusReturn = statusOfActionDataConfig();
		$feeCoinMsg = '';
		
		if(!isset($_POST['setodds'] )){exit();}
		
		if(!isUserLogin()){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['You_must_login_first']; 
			echo json_encode($action);
			exit();
		}
		
		if(mCrypt($_POST['tournament_id'].$_POST['match_id']) != $_POST['scrid']){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['something_is_wrong_here']; 
			echo json_encode($action);
			exit();
		}	
		 
		$i = 0;
		$canBet = true;
		foreach($_POST['setodds'] as $stake){
			$event_id = $_POST['eventid'][$i];
			$EVENT = $this->event_entity->init('id', $event_id);
			$MATCH = $this->match_entity->init('id', $EVENT->getMatchId());
			$i++;
			if($EVENT->getBetEnable() != 'yes'){
				$canBet = false;
				break;
			}
		}
		if($MATCH->getBetEnable() != 'yes' OR !$canBet){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['Bet_of_this_event_is_disable']; 
			echo json_encode($action);
			exit();
		}
		
		$minCoinStake = getMinCoinStake($_POST['tournament_id']);
		
		$totalStake = 0;
		
		foreach($_POST['setodds'] as $stake){
			if(! $this->phpvalidator->is_natural($stake) OR $stake <= 0){
				$action['status'] = 'err';
				$action['msg'] = $statusReturn['Place_bet_not_validated']; 
				echo json_encode($action);
				exit();
			}else if($stake < $minCoinStake){
				$action['status'] = 'err';
				$action['msg'] = str_replace( array('%1','%2') , array($stake,$minCoinStake), $statusReturn['your_stake_less_than_min_coin'] ); 
				echo json_encode($action);
				exit();
			}else{
				$totalStake += $stake;
			}
		}
		$USER = $this->user_entity->init("id", getUserId());
		if($USER->getCoin() < $totalStake){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['not_enough_coin_to_bet']; 
			echo json_encode($action);
			exit();
		}
		
		/*
		if(getMaxCoinsBetLimit() < $totalStake ){
			$action['status'] = 'err';
			$action['msg'] = str_replace( array('%1') , array(getMaxCoinsBetLimit()), $statusReturn['max_coins_bet_limit'] ); 
			echo json_encode($action);
			exit();
		}
		*/
		
		$limitCoinsStake = getLimitCoinStake($_POST['tournament_id']);
		if($limitCoinsStake > 0){
			$totalCoinsStaked = $totalStake + $this->user_tournament_entity->myCoinsStakedInTournament($USER->getId(), $_POST['tournament_id']);
			if( $totalCoinsStaked > $limitCoinsStake){
				$action['status'] = 'err';
				$action['msg'] = str_replace( array('%1','%2') , array($totalCoinsStaked,$limitCoinsStake), $statusReturn['your_stake_greater_than_limit_coin_stake'] ); 
				echo json_encode($action);
				exit();
			}
		}
		
		$TOURNAMENT = $this->tournament_entity->init('id',$_POST['tournament_id']);
		$isUserSetBetFee = $this->user_tournament_entity->isUserSetBetFee($USER->getId(), $TOURNAMENT->getId());
		if($isUserSetBetFee == 'no'){
			$changeMyCrashToINR = changeValueOfMoney($value= $USER->getCash(), $newCurrency='inr', $oldCurrency= $USER->getCurrencyCode());
			if( $changeMyCrashToINR < $TOURNAMENT->getEntryFee() ){
				$action['status'] = 'err';
				$action['msg'] = $statusReturn['not_enough_cash_to_join_tournament']; 
				echo json_encode($action);
				exit();
			}else{
				$myCashReturn = $changeMyCrashToINR - $TOURNAMENT->getEntryFee();
				$changeMyCashToCurrentCurrency = changeValueOfMoney($myCashReturn,$USER->getCurrencyCode(), 'inr' );
				$USER->setCash($changeMyCashToCurrentCurrency);
				$USER->update();
			}
			$this->user_tournament_entity->setUserBetFeeDeduct($USER->getId(), $TOURNAMENT->getId() , 'yes');
		}
	
		$i = 0;
		foreach($_POST['setodds'] as $stake){
			$event_id = $_POST['eventid'][$i];
			$EVENT = $this->event_entity->init('id', $event_id);
			$i++;
			
			$newBetId = $this->bet_entity->createNewBetEntity();
			$BET = $this->bet_entity->init('id',$newBetId);
			$BET->setEventId($event_id);
			$BET->setUserId($USER->getId());
			$BET->setStake($stake);
			$BET->setStatus('betting');
			$BET->setResult('pending');
			$BET->setBetTime(time());
			$BET->setTournamentId($_POST['tournament_id']);
			$BET->setMatchId($_POST['match_id']);
			$BET->setOdds($EVENT->getOdds());
			$BET->update();
		}
		$USER->setCoin($USER->getCoin()-$totalStake);
		$USER->update();
		
		$action['status'] = 'ok';
		$action['msg'] = $statusReturn['Bet_successfully'].'<br>'.$feeCoinMsg; 
		echo json_encode($action);
		exit();
	}
	
	function direct_bet(){
		$action = array();
		$statusReturn = statusOfActionDataConfig();
		if(!isUserLogin()){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['You_must_login_first']; 
			echo json_encode($action);
			exit();
		}
		$feeCoinMsg = '';
		
		$stake = $_POST['stake'];
		$points = $_POST['points'];
		$event_id = $_POST['event_id'];
		if(! $this->phpvalidator->is_natural($stake) OR $stake <= 0){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['Place_bet_not_validated']; 
			echo json_encode($action);
			exit();
		}	
		
		$USER = $this->user_entity->init("id", getUserId());
		if($USER->getCoin() < $stake){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['not_enough_coin_to_bet']; 
			echo json_encode($action);
			exit();
		}
		$EVENT = $this->event_entity->init('id', $event_id);
		$MATCH = $this->match_entity->init('id', $EVENT->getMatchId());
		$TOURNAMENT = $this->tournament_entity->init('id',$MATCH->getTournamentId());	
		$CAT = $this->event_category_entity->init('id',$EVENT->getEventCategoryId());
		
		if($MATCH->getBetEnable() != 'yes' OR $EVENT->getBetEnable() != 'yes' OR $CAT->getIsOpen() != 'yes'){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['Bet_of_this_event_is_disable']; 
			echo json_encode($action);
			exit();
		}
		
		$minCoinStake = getMinCoinStake($TOURNAMENT->getId());
		if($stake < $minCoinStake){
			$action['status'] = 'err';
			$action['msg'] = str_replace( array('%1','%2') , array($stake,$minCoinStake), $statusReturn['your_stake_less_than_min_coin'] ); 
			echo json_encode($action);
			exit();
		}
		
		if(getMaxCoinsBetLimit() < $stake + $this->bet_entity->getTotalStakeUserBettedCurrently($event_id,getUserId()) ){
			$action['status'] = 'err';
			$action['msg'] = str_replace( array('%1') , array(getMaxCoinsBetLimit()), $statusReturn['max_coins_bet_limit'] ); 
			echo json_encode($action);
			exit();
		}
		
		$limitCoinsStake = getLimitCoinStake($TOURNAMENT->getId());
		if($limitCoinsStake > 0){
			$totalCoinsStaked = $stake + $this->user_tournament_entity->myCoinsStakedInTournament($USER->getId(), $TOURNAMENT->getId());
			if( $totalCoinsStaked > $limitCoinsStake){
				$action['status'] = 'err';
				$action['msg'] = str_replace( array('%1','%2') , array($totalCoinsStaked,$limitCoinsStake), $statusReturn['your_stake_greater_than_limit_coin_stake'] ); 
				echo json_encode($action);
				exit();
			}
		}
	
		$isUserSetBetFee = $this->user_tournament_entity->isUserSetBetFee($USER->getId(), $TOURNAMENT->getId());
		if($isUserSetBetFee == 'no'){
			$changeMyCrashToINR = changeValueOfMoney($value= $USER->getCash(), $newCurrency='inr', $oldCurrency= $USER->getCurrencyCode());
			if( $changeMyCrashToINR < $TOURNAMENT->getEntryFee() ){
				$action['status'] = 'err';
				$action['msg'] = $statusReturn['not_enough_cash_to_join_tournament']; 
				echo json_encode($action);
				exit();
			}else{
				$myCashReturn = $changeMyCrashToINR - $TOURNAMENT->getEntryFee();
				$changeMyCashToCurrentCurrency = changeValueOfMoney($myCashReturn,$USER->getCurrencyCode(), 'inr' );
				$USER->setCash($changeMyCashToCurrentCurrency);
				$USER->update();
			}
			$this->user_tournament_entity->setUserBetFeeDeduct($USER->getId(), $TOURNAMENT->getId() , 'yes');
		}
		
		$newBetId = $this->bet_entity->createNewBetEntity();
		$BET = $this->bet_entity->init('id',$newBetId);
		$BET->setEventId($event_id);
		$BET->setUserId($USER->getId());
		$BET->setStake($stake);
		$BET->setStatus('betting');
		$BET->setResult('pending');
		$BET->setBetTime(time());
		$BET->setTournamentId($TOURNAMENT->getId());
		$BET->setMatchId($MATCH->getId());
		$BET->setOdds($EVENT->getOdds());
		$BET->update();
		
		$USER->setCoin($USER->getCoin()-$stake);
		$USER->update();
		
		$action['status'] = 'ok';
		$action['msg'] = $statusReturn['Bet_successfully'].'<br>'.$feeCoinMsg; 
		echo json_encode($action);
		exit();
	}
	
	function direct_bet_manual(){
		$action = array();
		$statusReturn = statusOfActionDataConfig();
		$feeCoinMsg = '';
		
		if(!isUserLogin()){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['You_must_login_first']; 
			echo json_encode($action);
			exit();
		}
		
		$stake = $_POST['stake'];
		$points = $_POST['points'];
		$cat_manual_id = $_POST['cat_id'];
		$value = $_POST['value'];
		$split = explode('{[|SEPSTR|]}',$value);
		$subStrArray = array();
		foreach($split as $text){
			$subStrArray[] = substr($text, 0 ,getMaxLengthWordManualBoxDataConfig());
		}
		$value = implode('{[|SEPSTR|]}',$subStrArray);
		
		$MANUAL_CAT = $this->category_manual_entity->init('key_id', $cat_manual_id);
		if($MANUAL_CAT->getType() == 'range'){
			$array = explode('{[|SEPSTR|]}',$value);
			
			if($array[0] + $MANUAL_CAT->getRangeDenote() != $array[1]){
				$action['status'] = 'err';
				$action['msg'] = $statusReturn['answer_value_not_valid']; 
				echo json_encode($action);
				exit();
			}
		}else{
			 $array = array_filter( explode('{[|SEPSTR|]}',$value) );
			  
			 if(sizeof($array) < $MANUAL_CAT->getNumberBoxes()){
				$action['status'] = 'err';
				$action['msg'] = $statusReturn['fill_full_boxes_err']; 
				echo json_encode($action);
				exit();
			 }
		}
    
		if(! $this->phpvalidator->is_natural($stake) OR $stake <= 0){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['Place_bet_not_validated']; 
			echo json_encode($action);
			exit();
		}	
		
		$USER = $this->user_entity->init("id", getUserId());
		if($USER->getCoin() < $stake){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['not_enough_coin_to_bet']; 
			echo json_encode($action);
			exit();
		}
		
		$MATCH = $this->match_entity->init('id', $MANUAL_CAT->getMatchId());
		$TOURNAMENT = $this->tournament_entity->init('id',$MATCH->getTournamentId());	
		
		if($MATCH->getBetEnable() != 'yes' OR $MANUAL_CAT->getIsOpen() != 'yes'){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['Bet_of_this_event_is_disable']; 
			echo json_encode($action);
			exit();
		}
		
		$minCoinStake = getMinCoinStake($TOURNAMENT->getId());
		if($stake < $minCoinStake){
			$action['status'] = 'err';
			$action['msg'] = str_replace( array('%1','%2') , array($stake,$minCoinStake), $statusReturn['your_stake_less_than_min_coin'] ); 
			echo json_encode($action);
			exit();
		}
		
		if(getMaxCoinsBetLimit() < $stake + $this->bet_manual_entity->getTotalStakeUserBettedCurrently($cat_manual_id,getUserId()) ){
			$action['status'] = 'err';
			$action['msg'] = str_replace( array('%1') , array(getMaxCoinsBetLimit()), $statusReturn['max_coins_bet_limit'] ); 
			echo json_encode($action);
			exit();
		}
		
		$limitCoinsStake = getLimitCoinStake($TOURNAMENT->getId());
		if($limitCoinsStake > 0){
			$totalCoinsStaked = $stake + $this->user_tournament_entity->myCoinsStakedInTournament($USER->getId(), $TOURNAMENT->getId());
			if( $totalCoinsStaked > $limitCoinsStake){
				$action['status'] = 'err';
				$action['msg'] = str_replace( array('%1','%2') , array($totalCoinsStaked,$limitCoinsStake), $statusReturn['your_stake_greater_than_limit_coin_stake'] ); 
				echo json_encode($action);
				exit();
			}
		}
	
		$isUserSetBetFee = $this->user_tournament_entity->isUserSetBetFee($USER->getId(), $TOURNAMENT->getId());
		if($isUserSetBetFee == 'no'){
			$changeMyCrashToINR = changeValueOfMoney($USER->getCash(), $newCurrency='inr', $oldCurrency= $USER->getCurrencyCode());
			if( $changeMyCrashToINR < $TOURNAMENT->getEntryFee() ){
				$action['status'] = 'err';
				$action['msg'] = $statusReturn['not_enough_cash_to_join_tournament']; 
				echo json_encode($action);
				exit();
			}else{
				$myCashReturn = $changeMyCrashToINR - $TOURNAMENT->getEntryFee();
				$changeMyCashToCurrentCurrency = changeValueOfMoney($myCashReturn,$USER->getCurrencyCode(), 'inr' );
				$USER->setCash($changeMyCashToCurrentCurrency);
				$USER->update();
			}
			$this->user_tournament_entity->setUserBetFeeDeduct($USER->getId(), $TOURNAMENT->getId() , 'yes');
		}
		
		$newManualBetId = $this->bet_manual_entity->createNewBetEntity();
		$BET = $this->bet_manual_entity->init('id',$newManualBetId);
		$BET->setCategoryId($MANUAL_CAT->getKeyId());
		$BET->setUserId($USER->getId());
		$BET->setStake($stake);
		$BET->setStatus('betting');
		$BET->setResult('pending');
		$BET->setBetTime(time());
		$BET->setTournamentId($TOURNAMENT->getId());
		$BET->setMatchId($MATCH->getId());
		$BET->setOdds($MANUAL_CAT->getOdds());
		$BET->setAnswer($value);
		$BET->update();
		
		$USER->setCoin($USER->getCoin()-$stake);
		$USER->update();
		
		$action['status'] = 'ok';
		$action['msg'] = $statusReturn['Bet_successfully'].'<br>'.$feeCoinMsg;  
		echo json_encode($action);
		exit();
	}
	
	function direct_bet_tournament(){
		$action = array();
		$statusReturn = statusOfActionDataConfig();
		if(!isUserLogin()){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['You_must_login_first']; 
			echo json_encode($action);
			exit();
		}
		$feeCoinMsg = '';
		
		$stake = $_POST['stake'];
		$points = $_POST['points'];
		$event_id = $_POST['event_id'];
		if(! $this->phpvalidator->is_natural($stake) OR $stake <= 0){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['Place_bet_not_validated']; 
			echo json_encode($action);
			exit();
		}	
		
		$USER = $this->user_entity->init("id", getUserId());
		if($USER->getCoin() < $stake){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['not_enough_coin_to_bet']; 
			echo json_encode($action);
			exit();
		}
		$EVENT = $this->event_tournament_entity->init('id', $event_id);
		$TOURNAMENT = $this->tournament_entity->init('id',$EVENT->getTournamentId());	
		$CAT = $this->event_category_tournament_entity->init('id',$EVENT->getTournamentEventCategoryId());
		
		if( $EVENT->getBetEnable() != 'yes' OR $CAT->getIsOpen() != 'yes' ){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['Bet_of_this_event_is_disable']; 
			echo json_encode($action);
			exit();
		}
		
		$minCoinStake = getMinCoinStake($TOURNAMENT->getId());
		if($stake < $minCoinStake){
			$action['status'] = 'err';
			$action['msg'] = str_replace( array('%1','%2') , array($stake,$minCoinStake), $statusReturn['your_stake_less_than_min_coin'] ); 
			echo json_encode($action);
			exit();
		}
		
		if(getMaxCoinsBetLimit() < $stake + $this->bet_tournament_entity->getTotalStakeUserBettedCurrently($event_id,getUserId()) ){
			$action['status'] = 'err';
			$action['msg'] = str_replace( array('%1') , array(getMaxCoinsBetLimit()), $statusReturn['max_coins_bet_limit'] ); 
			echo json_encode($action);
			exit();
		}
		
		$limitCoinsStake = getLimitCoinStake($TOURNAMENT->getId());
		if($limitCoinsStake > 0){
			$totalCoinsStaked = $stake + $this->user_tournament_entity->myCoinsStakedInTournament($USER->getId(), $TOURNAMENT->getId());
			if( $totalCoinsStaked > $limitCoinsStake){
				$action['status'] = 'err';
				$action['msg'] = str_replace( array('%1','%2') , array($totalCoinsStaked,$limitCoinsStake), $statusReturn['your_stake_greater_than_limit_coin_stake'] ); 
				echo json_encode($action);
				exit();
			}
		}
	
		$isUserSetBetFee = $this->user_tournament_entity->isUserSetBetFee($USER->getId(), $TOURNAMENT->getId());
		if($isUserSetBetFee == 'no'){
			$changeMyCrashToINR = changeValueOfMoney($value= $USER->getCash(), $newCurrency='inr', $oldCurrency= $USER->getCurrencyCode());
			if( $changeMyCrashToINR < $TOURNAMENT->getEntryFee() ){
				$action['status'] = 'err';
				$action['msg'] = $statusReturn['not_enough_cash_to_join_tournament']; 
				echo json_encode($action);
				exit();
			}else{
				$myCashReturn = $changeMyCrashToINR - $TOURNAMENT->getEntryFee();
				$changeMyCashToCurrentCurrency = changeValueOfMoney($myCashReturn,$USER->getCurrencyCode(), 'inr' );
				$USER->setCash($changeMyCashToCurrentCurrency);
				$USER->update();
			}
			$this->user_tournament_entity->setUserBetFeeDeduct($USER->getId(), $TOURNAMENT->getId() , 'yes');
		}
		
		$newBetId = $this->bet_tournament_entity->createNewBetEntity();
		$BET = $this->bet_tournament_entity->init('id',$newBetId);
		$BET->setEventId($event_id);
		$BET->setUserId($USER->getId());
		$BET->setStake($stake);
		$BET->setStatus('betting');
		$BET->setResult('pending');
		$BET->setBetTime(time());
		$BET->setTournamentId($TOURNAMENT->getId());
		$BET->setOdds($EVENT->getOdds());
		$BET->update();
		
		$USER->setCoin($USER->getCoin()-$stake);
		$USER->update();
		
		$action['status'] = 'ok';
		$action['msg'] = $statusReturn['Bet_successfully'].'<br>'.$feeCoinMsg; 
		echo json_encode($action);
		exit();
	}
	
	function direct_bet_manual_tournament(){
		$action = array();
		$statusReturn = statusOfActionDataConfig();
		$feeCoinMsg = '';
		
		if(!isUserLogin()){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['You_must_login_first']; 
			echo json_encode($action);
			exit();
		}
		
		$stake = $_POST['stake'];
		$points = $_POST['points'];
		$cat_manual_id = $_POST['cat_id'];
		$value = $_POST['value'];
		$split = explode('{[|SEPSTR|]}',$value);
		$subStrArray = array();
		foreach($split as $text){
			$subStrArray[] = substr($text, 0 ,getMaxLengthWordManualBoxDataConfig());
		}
		$value = implode('{[|SEPSTR|]}',$subStrArray);
		
		$MANUAL_CAT = $this->category_manual_tournament_entity->init('key_id', $cat_manual_id);
		if($MANUAL_CAT->getType() == 'range'){
			$array = explode('{[|SEPSTR|]}',$value);
			
			if($array[0] + $MANUAL_CAT->getRangeDenote() != $array[1]){
				$action['status'] = 'err';
				$action['msg'] = $statusReturn['answer_value_not_valid']; 
				echo json_encode($action);
				exit();
			}
		}else{
			 $array = array_filter( explode('{[|SEPSTR|]}',$value) );
			  
			 if(sizeof($array) < $MANUAL_CAT->getNumberBoxes()){
				$action['status'] = 'err';
				$action['msg'] = $statusReturn['fill_full_boxes_err']; 
				echo json_encode($action);
				exit();
			 }
		}
    
		if(! $this->phpvalidator->is_natural($stake) OR $stake <= 0){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['Place_bet_not_validated']; 
			echo json_encode($action);
			exit();
		}	
		
		$USER = $this->user_entity->init("id", getUserId());
		if($USER->getCoin() < $stake){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['not_enough_coin_to_bet']; 
			echo json_encode($action);
			exit();
		}
		
		if( $MANUAL_CAT->getIsOpen() != 'yes' ){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['Bet_of_this_event_is_disable']; 
			echo json_encode($action);
			exit();
		}
		
		$TOURNAMENT = $this->tournament_entity->init('id',$MANUAL_CAT->getTournamentId());	
		
		$minCoinStake = getMinCoinStake($TOURNAMENT->getId());
		if($stake < $minCoinStake){
			$action['status'] = 'err';
			$action['msg'] = str_replace( array('%1','%2') , array($stake,$minCoinStake), $statusReturn['your_stake_less_than_min_coin'] ); 
			echo json_encode($action);
			exit();
		}
		
		if(getMaxCoinsBetLimit() < $stake + $this->bet_manual_tournament_entity->getTotalStakeUserBettedCurrently($cat_manual_id,getUserId()) ){
			$action['status'] = 'err';
			$action['msg'] = str_replace( array('%1') , array(getMaxCoinsBetLimit()), $statusReturn['max_coins_bet_limit'] ); 
			echo json_encode($action);
			exit();
		}
		
		$limitCoinsStake = getLimitCoinStake($TOURNAMENT->getId());
		if($limitCoinsStake > 0){
			$totalCoinsStaked = $stake + $this->user_tournament_entity->myCoinsStakedInTournament($USER->getId(), $TOURNAMENT->getId());
			if( $totalCoinsStaked > $limitCoinsStake){
				$action['status'] = 'err';
				$action['msg'] = str_replace( array('%1','%2') , array($totalCoinsStaked,$limitCoinsStake), $statusReturn['your_stake_greater_than_limit_coin_stake'] ); 
				echo json_encode($action);
				exit();
			}
		}
	
		$isUserSetBetFee = $this->user_tournament_entity->isUserSetBetFee($USER->getId(), $TOURNAMENT->getId());
		if($isUserSetBetFee == 'no'){
			$changeMyCrashToINR = changeValueOfMoney( $USER->getCash(), $newCurrency='inr', $oldCurrency= $USER->getCurrencyCode());
			if( $changeMyCrashToINR < $TOURNAMENT->getEntryFee() ){
				$action['status'] = 'err';
				$action['msg'] = $statusReturn['not_enough_cash_to_join_tournament']; 
				echo json_encode($action);
				exit();
			}else{
				$myCashReturn = $changeMyCrashToINR - $TOURNAMENT->getEntryFee();
				$changeMyCashToCurrentCurrency = changeValueOfMoney($myCashReturn,$USER->getCurrencyCode(), 'inr' );
				$USER->setCash($changeMyCashToCurrentCurrency);
				$USER->update();
			}
			$this->user_tournament_entity->setUserBetFeeDeduct($USER->getId(), $TOURNAMENT->getId() , 'yes');
		}
		
		$BET = $this->bet_manual_tournament_entity->createNewBetEntity();
		$BET->setCategoryId($MANUAL_CAT->getKeyId());
		$BET->setUserId($USER->getId());
		$BET->setStake($stake);
		$BET->setStatus('betting');
		$BET->setResult('pending');
		$BET->setBetTime(time());
		$BET->setTournamentId($TOURNAMENT->getId());
		$BET->setOdds($MANUAL_CAT->getOdds());
		$BET->setAnswer($value);
		$BET->update();
		
		$USER->setCoin($USER->getCoin()-$stake);
		$USER->update();
		
		$action['status'] = 'ok';
		$action['msg'] = $statusReturn['Bet_successfully'].'<br>'.$feeCoinMsg;  
		echo json_encode($action);
		exit();
	}
	
	function convert(){
		$action = array();
		$statusReturn = statusOfActionDataConfig();
		if(!isUserLogin()){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['You_must_login_first']; 
			echo json_encode($action);
			exit();
		}
		
		if(mCrypt($_POST['tournament_id'].$_POST['match_id']) != $_POST['scrid']){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['something_is_wrong_here']; 
			echo json_encode($action);
			exit();
		}	
		
		if(! $this->phpvalidator->is_natural($_POST['point']) OR $_POST['point']<= 0){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['Point_to_convert_not_valid']; 
			echo json_encode($action);
			exit();
		}
		
		$USER = $this->user_entity->init("id", getUserId());
		$object_result = $this->user_tournament_entity->findUserInTournament($USER->getId(), ($_POST['tournament_id']));
		if($object_result){
			$mypoint = $object_result->point;
		}else{
			$mypoint = 0; //prePointForUserBetEvent($_POST['tournament_id']);
		}
		
		$_POST['point'] = intval($_POST['point']);
		
		if($mypoint < $_POST['point']){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['point_not_enough_to_convert']; 
			echo json_encode($action);
			exit();
		}
		
		if(getConvertionStatus($_POST['match_id']) != 'yes'){
			$action['status'] = 'err';
			$action['msg'] = $statusReturn['convertion_timeout']; 
			echo json_encode($action);
			exit();
		}
		
		$rate_array = explode(':',rateConvertion($_POST['match_id']));
		$point = $rate_array[0];
		$coin = $rate_array[1];
		
		$getCoin = floor($coin*$_POST['point']/$point);
		$pointUsed = $point*$getCoin/$coin; 
		
		$myPointReturn = $_POST['point'] - $pointUsed;
	 	
		if($object_result){
			$USER_TOURNAMENT = $this->user_tournament_entity->init('id',$object_result->id) ;
			$USER_TOURNAMENT->setPoint($mypoint - $pointUsed);
			$USER_TOURNAMENT->update();
			
			$USER->setCoin( $USER->getCoin() + $getCoin);
			$USER->update();
		}else{
			$insertId = $this->user_tournament_entity->createNewUserTournamentEntity();
			$USER_TOURNAMENT = $this->user_tournament_entity->init('id',$insertId) ;
			$USER_TOURNAMENT->setPoint($mypoint - $pointUsed);
			$USER_TOURNAMENT->setTournamentId($_POST['tournament_id']);
			$USER_TOURNAMENT->setUserId($USER->getId());
			$USER_TOURNAMENT->update();
			
			$USER->setCoin( $USER->getCoin() + $getCoin);
			$USER->update();
		}
	 
		$historyId = $this->convertion_history_entity->createNewConvertionHistoryEntity();
		$HISTORY_CONVERTION = $this->convertion_history_entity->init('id',$historyId);
		$HISTORY_CONVERTION->setUserId($USER->getId());
		$HISTORY_CONVERTION->setPoint( $pointUsed );
		$HISTORY_CONVERTION->setCoin($getCoin);
		$HISTORY_CONVERTION->setTournamentId($_POST['tournament_id']);
		$HISTORY_CONVERTION->setTime( time() );
		$HISTORY_CONVERTION->setConvertRate( rateConvertion($_POST['match_id']) );
		$HISTORY_CONVERTION->setMatchId( $_POST['match_id'] );
		$HISTORY_CONVERTION->update();
		
		$action['status'] = 'ok';
		$action['msg'] = str_replace(array('%1','%2','%3'),array($_POST['point'],$getCoin,$myPointReturn),$statusReturn['convert_successfully']); 
		echo json_encode($action);
		exit();
	}
	
	
	
	
	
	
	
	
	
}